package com.example.myapplication.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.example.myapplication.R;
import com.example.myapplication.bean.Song;

import java.util.List;

/**
 * @author ayatsuji
 * @date 2021-09-22 22:26
 * @since
 */
public class SongListAdapter extends BaseAdapter {

    private List<Song> songs;
    private Context context;

    public SongListAdapter(List<Song> songs, Context context) {
        this.songs = songs;
        this.context = context;
    }

    @Override
    public int getCount() {
        return songs.size();
    }

    @Override
    public Object getItem(int position) {
        return songs.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.song_item, parent, false);
        }
        Song song = (Song) getItem(position);
        ((TextView) convertView.findViewById(R.id.songTitle)).setText(song.getTitle());
        ((TextView) convertView.findViewById(R.id.songLength)).setText(song.getLength());
        ((TextView) convertView.findViewById(R.id.songSinger)).setText(song.getSinger());
        return convertView;
    }
}
